package com.group01.community.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.SysDictData;
import com.group01.community.entity.SysRole;
import com.group01.community.entity.excel.SysRoleExcel;
import com.group01.community.entity.vo.SysRoleVo;
import com.group01.community.service.SysRoleService;
import com.group01.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */

@Api(tags = "角色模块")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation("分页查询角色信息")
    @GetMapping("/list/")
    public ResponseResult<IPage<SysRole>> getAllZyCommunityByDeptId(Long current, Long size, String roleName, String roleKey, String status, String startTime, String endTime,String userName ) {
        return ResponseResult.ok(sysRoleService.getRoleList(current, size, roleName, roleKey, status, startTime, endTime,userName));
    }

    @ApiOperation("分页查询角色基础信息")
    @GetMapping("/roleList")
    public ResponseResult<IPage<SysRole>> roleList(Long current, Long size, String roleName, String roleKey, String status, String startTime, String endTime,String userName ) {
        return ResponseResult.ok(sysRoleService.getBaseRoleList(current, size, roleName, roleKey, status, startTime, endTime,userName));
    }

    @ApiOperation("查询所有状态")
    @GetMapping("/status")
    public ResponseResult<Object[]>  getAllStatus(){
        Object[] objects = sysRoleService.getAllByStatus(3L).toArray();
        return ResponseResult.ok(objects);
    }

    @ApiOperation("添加角色信息")
    @PostMapping("/add")
    public ResponseResult<String> addRole(@RequestBody SysRole sysRole) {
    //一个用户下面多个角色
        sysRole.setCreateTime(LocalDateTime.now());
        sysRoleService.addRole(sysRole);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("修改角色状态")
    @PutMapping("/updateRoleStatus")
    public ResponseResult<String> updateRoleStatus(@RequestBody SysRole sysRole){

        sysRole.setUpdateTime(LocalDateTime.now());
        sysRoleService.updateById(sysRole);


        return ResponseResult.ok("ok");
    }


    @ApiOperation("添加角色信息")
    @PostMapping("/update")
    public ResponseResult<String> updateRole(@RequestBody SysRole sysRole){
        sysRole.setUpdateTime(LocalDateTime.now());
        sysRoleService.updateRole(sysRole);
        return ResponseResult.ok("ok");
    }
    @ApiOperation("删除角色信息")
    @DeleteMapping("/delete")
    public ResponseResult<String> deleteRole(String roleId){
        sysRoleService.deleteRole(roleId);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("批量删除角色信息")
    @DeleteMapping("/BatchDelete")
    public ResponseResult<String> deleteBatchRole(@RequestBody Integer[] ids){
        sysRoleService.deleteBatchRole(ids);
        return ResponseResult.ok("ok");
    }


    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long current, Long size, String roleName, String roleKey,
                             String status, String startTime, String endTime,String userName)  {
        // 设置下载信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // URLEncoder.encode可以防止中文乱码
        String fileName= null;
        try {
            fileName = URLEncoder.encode("角色信息","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
        // 查询数据信息
        IPage<SysRole> roleIPage = sysRoleService.getBaseRoleList(current, size, roleName, roleKey, status, startTime, endTime,userName);
        List<SysRole> roleList = roleIPage.getRecords();
        //将SysRole映射为导出的实体
        List<SysRoleExcel> sysRoleExcelList=new ArrayList<>();
        for (SysRole role:roleList){
            SysRoleExcel sysRoleExcel = new SysRoleExcel();
            BeanUtils.copyProperties(role,sysRoleExcel);
            sysRoleExcelList.add(sysRoleExcel);
        }

        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(),SysRoleExcel.class).sheet("角色信息")
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(25)) // 简单的列宽策略，列宽25
                    .registerWriteHandler(new SimpleRowHeightStyleStrategy((short)35,(short)20)) // 简单的行高策略：头行高30，内容行高20
                    .doWrite(sysRoleExcelList);


        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    @ApiOperation("修改")
    @PutMapping("/pow")
    public ResponseResult<String> updatePowRole(@RequestBody SysRole sysRole){
        sysRoleService.updateRolePow(sysRole);
        return ResponseResult.ok("ok");
    }



}

